#include"SeqList.h"
void first(SeqList * p)
{
	p->a = NULL;
	p->size = 0;
	p->capacity = 0;
}
void endintsert(SeqList * x, SLDatatype y)
{
	assert(x);
	if (x->size == x->capacity)
	{
		
		x->capacity=x->capacity==0?4: x->capacity * 2;
		SLDatatype * j = (SLDatatype*)realloc(x->a, sizeof(SLDatatype)*x->capacity);
		if (j == NULL)
		{
			exit(-1);
		}
		else
			x->a = j;
	}
	x->a[x->size] = y;
	(x->size)++;
}
void SeqListPrint(SeqList*x)
{
	int j = 0;
	for (j = 0; j < x->size; j++)
		printf("%d", x->a[j]);
}
void SeqListCheckCapacity(SeqList* x)
{
	assert(x);
	if (x->size == x->capacity)
	{

		x->capacity = x->capacity == 0 ? 4 : x->capacity * 2;
		SLDatatype* j = (SeqList*)realloc(x->a, sizeof(SLDatatype) * x->capacity);
	}
}
void SeqListInsert(SeqList* x, size_t y, SLDatatype z)
{
	if (y >= 0 && y < x->size)
	{
		void big(x); 
		size_t m = x ->size ;
		while (m > y)
		{
			x->a[m-1] = x->a[m];
			--m;
		}
		x->a[y] = z;
	 }
	(x->size)++;
}
void SeqListDestroy(SeqList* psl)
{
	assert(psl);
	free(psl->a);
	psl->a = NULL;
	psl->size = 0;
	psl->capacity = 0;
}
void SeqListErase(SeqList* psl, size_t pos)
{
	assert(psl);
	size_t begin = pos + 1;
	if (pos < psl->size)
	{
		return;
	}
	   while(begin<psl->size)
	{
		   psl->a[begin - 1] = psl->a[begin];
		   ++begin;
	}
	   --psl->size;
}
SeqListFind(SeqList* psl, SLDatatype x)
{
	assert(psl);
	int pos = 0;
	for (pos = 0; pos < psl->size; pos++)
	{
		if (psl->a[pos] == x)
		{
			return pos;
		}
		
	}
	return -1;
}
void SeqListInit(SeqList* psl,SLDatatype x)
{
	assert(psl);
	SeqListCheckCapacity(psl);
	int end = psl->size;
	while (end>0)
	{
		psl->a[end] = psl->a[end - 1];
		--end;
	}
	psl->a = x;
	++psl->size;
}
void SeqListPopFront(SeqList* psl)
{
	assert(psl);
	
	if (psl->size > 0)
	{
		int begin = 0;
		while (begin < psl->size)
		{
			psl->a[begin] = psl->a[begin + 1];
			begin++;
		}
		--psl->size;
	}
}
void SeqListpushback(SeqList* psl, SLDatatype x)
{
	assert(psl);
	SeqListCheckCapacity(psl);
	int pos = psl->size;
	psl->a[pos] = x;
	++psl->size;
}
void SeqListPopBack(SeqList*psl)
{
	assert(psl);
	if(psl->size>0)
	--psl->size;
}

